﻿using System;
using System.Web.UI.WebControls;

using HsTec.Mobile.Green.Util;
using HsTec.Mobile.Green.BLL;
using Baimasoft.Manage.SqlServerDAL;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web;
using System.IO;
using System.Data;
using System.Web.UI;
using System.Text;

public partial class ReportAnalysis_AreaCategoryManage : System.Web.UI.Page
{
    AreaCategoryBLL bll = new AreaCategoryBLL();
    ForecastBusinessBLL bll2 = new ForecastBusinessBLL();
    Param MyP = new Param();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        if (!this.IsPostBack)
        {
            try
            {
                Indicator.Text = Request.QueryString["sIndicator"].ToString();
            }
            catch
            {

            }
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue");
            this.BindsltArea();
            this.txtYearsDataBind();
            GridView2.Attributes.Add("BorderColor", "LightSkyBlue");
            this.BindsltArea2();
            this.txtYearsDataBind2();
        }
    }

    private void BindGridViewData2()
    {
        if (txtYears2.SelectedValue.Trim() == "")
        {
            Response.Write("<script language=javascript>alert('请先选择年月后设置地市标杆值！');</script>");
            return;
        }
        GridView2.DataSource = bll2.GetBenchMarkList(GetSearchString2());
        GridView2.DataBind();
        HiddenID.Value = "";
    }

    private void BindsltArea2()
    {
        sltArea2.DataTextField = "Name";
        sltArea2.DataValueField = "ID";
        sltArea2.DataSource = bll2.GetAreaList();
        sltArea2.DataBind();
        sltArea2.Items.Insert(0, "全省");
        sltArea2.Items[0].Value = "";
    }
    private void BindGridViewData()
    {
        if (txtYears.SelectedValue.Trim() == "")
        {
            Response.Write("<script language=javascript>alert('请先选择年月后再查询！');</script>");
            return;
        }
        GridView1.DataSource = bll.GetList(GetSearchString());
        GridView1.DataBind();
        HiddenID.Value = "";
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    private string GetSearchString()
    {
        string searchStr = "(1=1)";

        string sddlRegionid = sltArea.SelectedValue.ToString().Trim();
        if (sddlRegionid != "")
        {
            searchStr += " and Area_ID  ='" + sddlRegionid + "' ";
        }
        if (txtYears.SelectedValue.Trim() != "")
        {
            searchStr += " and Years  ='" + txtYears.SelectedValue.Trim() + "' ";
        }
        return searchStr;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(HiddenID.Value.Trim()))
        {
            bll.Delete(HiddenID.Value.TrimEnd(','));
            BindGridViewData();
        }
    }
    protected void linkBtnGo_Click(object sender, EventArgs e)
    {

    }
    private void BindsltArea()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetProvince();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择省份...");
        sltArea.Items[0].Value = "";
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        bll.GetList(GetSearchString()).Sort = e.SortExpression;
        this.BindGridViewData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGridViewData();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridViewData();
    }
    private void txtYearsDataBind()
    {
        txtYears.DataTextField = "Name";
        txtYears.DataValueField = "Name";
        txtYears.DataSource = MyP.GetCodeByParentCode("1");
        txtYears.DataBind();
        txtYears.Items.Insert(0, "请选择...");
        txtYears.Items[0].Value = "";
    }
    private void txtYearsDataBind2()
    {
        txtYears2.DataTextField = "Name";
        txtYears2.DataValueField = "Name";
        txtYears2.DataSource = MyP.GetCodeByParentCode("1");
        txtYears2.DataBind();
        txtYears2.Items.Insert(0, "请选择...");
        txtYears2.Items[0].Value = "";
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.BindGridViewData2();
    }
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        bll2.GetList(GetSearchString2()).Sort = e.SortExpression;
        this.BindGridViewData();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            HtmlSelect sltType = (HtmlSelect)(e.Row.Cells[5].FindControl("sltType"));
            sltType.DataTextField = "Name";
            sltType.DataValueField = "ID";
            sltType.DataSource = bll.GetSltList(txtYears2.SelectedValue.Trim());
            sltType.DataBind();
            sltType.Items.Insert(0, "请选择...");
            sltType.Items[0].Value = "";

            Label lblValue = (Label)(e.Row.Cells[8].FindControl("lblValue"));
            lblValue.Text = e.Row.Cells[9].Text;

            sltType.Value = e.Row.Cells[7].Text + "|" + e.Row.Cells[9].Text;
            sltType.Attributes.Add("onchange", "sltChange(this.value," + lblValue.ClientID + ");");
        }
    }
    protected void btnAdd2_ServerClick(object sender, EventArgs e)
    {
        string[] ary = new string[2];
        Dictionary<string, string> dc = new Dictionary<string, string>();
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            HtmlSelect sltType = (HtmlSelect)(GridView2.Rows[i].Cells[6].FindControl("sltType"));
            ary = sltType.Value.Split('|');
            dc.Add(GridView2.Rows[i].Cells[0].Text.ToString(), ary[0]);
        }
        int iBack = bll2.SetBenchMark(dc);
        if (iBack == 1)
        {
            Response.Write("<script language=javascript>alert('修改成功！');</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('修改失败！');</script>");
        }
        BindGridViewData2();
    }
    protected void btnSearch2_Click(object sender, EventArgs e)
    {
        BindGridViewData2();
    }
    private string GetSearchString2()
    {
        string searchStr = "(1=1)";

        string sddlRegionid = sltArea2.SelectedValue.ToString().Trim();
        if (sddlRegionid != "")
        {
            searchStr += " and Area_ID  ='" + sddlRegionid + "' ";
        }
        if (txtYears2.SelectedValue.Trim() != "")
        {
            searchStr += " and Years  ='" + txtYears2.SelectedValue.Trim() + "' ";
        }
        return searchStr;
    }

    protected void btnExport2_ServerClick(object sender, EventArgs e)
    {
        try
        {
            HttpContext curContext = System.Web.HttpContext.Current;
            StringWriter strWriter = new StringWriter();
            System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            curContext.Response.Charset = "GB2312";
            DataView mydv = bll2.BenchMarkReport(GetSearchString2());
            GridView GV = new GridView();
            GV.DataSource = mydv;
            GV.DataBind();
            GV.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }
        catch
        {

        }
        //string filename = "标杆值";
        //Response.Clear();
        //Response.Charset = "gb2312";
        //Response.ContentType = "application/vnd.ms-excel";
        //Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(filename) + ".xls\"");

        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //GridView1.RenderControl(htw);

        //Response.Write(sw.ToString());
        //Response.Flush();
        //Response.End();
    }
}